package com.lottery.im.module.chat.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import butterknife.BindView;
import butterknife.OnClick;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.MemberVipInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.util.StatusBarHelper;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.ext.group.TIMGroupManagerExt;
import java.util.ArrayList;
import java.util.List;

/**
 * 禁言界面
 */
public class SilenceFragment extends BaseSwipeFragment {

  private final static String INFO_KEY = "INFO_KEY";

  public static SilenceFragment create(MemberVipInfo info) {
    SilenceFragment fragment = new SilenceFragment();
    Bundle b = new Bundle();
    b.putSerializable(INFO_KEY, info);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.list) RecyclerView mList;
  @BindView(R.id.et_time) EditText mEtTime;

  private MemberVipInfo mVipInfo;

  private int mSelectPosition = -1;

  private List<String> mItem;
  private TimeAdapter mAdapter;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_silence;
  }

  @Override protected void initPresenter() {

    StatusBarHelper.setStatusBarDarkMode(getActivity());

    mVipInfo = (MemberVipInfo) getArguments().getSerializable(INFO_KEY);
    mList.setLayoutManager(new LinearLayoutManager(mActivity));
    RecyclerViewDivider.with(mActivity)
        .color(ContextCompat.getColor(mActivity, R.color.windowBackground))
        .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider()
        .build()
        .addTo(mList);

    mItem = new ArrayList<>();
    mItem.add("关闭禁言");
    mItem.add("10分钟");
    mItem.add("1小时");
    mItem.add("12小时");
    mItem.add("1天");
    mItem.add("永久");

    mAdapter = new TimeAdapter(mItem);
    mAdapter.setOnItemClickListener((adapter, view, position) -> {
      mEtTime.setText("");
      mSelectPosition = position;
      mAdapter.notifyDataSetChanged();
    });
    mList.setAdapter(mAdapter);
  }

  @OnClick({ R.id.iv_back, R.id.tv_ok }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_ok:

        String time = mEtTime.getText().toString();
        long timeLong = 0;
        if (TextUtils.isEmpty(time)) {
          if (mSelectPosition == -1) {
            popBackStack();
            return;
          } else {
            switch (mSelectPosition) {
              case 0:
                timeLong = 0;
                break;
              case 1:
                timeLong = 60 * 10;
                break;
              case 2:
                timeLong = 60 * 10 * 60;
                break;
              case 3:
                timeLong = 60 * 10 * 60 * 12;
                break;
              case 4:
                timeLong = 60 * 10 * 60 * 24;
                break;
              case 5:
                timeLong = 10000000000000000l;
                break;
            }
          }
        } else {
          timeLong = Long.valueOf(time);
        }

        showLoadingDialog();
        TIMGroupManagerExt.ModifyMemberInfoParam param =
            new TIMGroupManagerExt.ModifyMemberInfoParam(mVipInfo.getGroupId(), mVipInfo.getId());
        param.setSilence(timeLong);

        TIMGroupManagerExt.getInstance().modifyMemberInfo(param, new TIMCallBack() {
          @Override
          public void onError(int code, String desc) {
            cancelLoadingDialog();
            showToast("操作失败,请稍后再试～");
          }
          @Override
          public void onSuccess() {
            cancelLoadingDialog();
            popBackStack();
          }
        });
        break;
    }
  }

  class TimeAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    public TimeAdapter(@Nullable List<String> data) {
      super(R.layout.item_silence_time, data);
    }

    @Override protected void convert(BaseViewHolder helper, String item) {
      helper.setText(R.id.tv_time, item);
      if (helper.getLayoutPosition() == mSelectPosition) {
        helper.setGone(R.id.iv_check, true);
      } else {
        helper.setGone(R.id.iv_check, false);
      }
    }
  }
}
